package gene.android.equationcalculator;

import java.util.Stack;
import gene.android.equationcalculator.exp4j.Calculable;
import gene.android.equationcalculator.exp4j.ExpressionBuilder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.TextView;

public class EquationCalculator extends Activity {

	public static final double PI_VALUE = 3.141592654;
	public static final double E_VALUE = 2.718281828;

	public static final String KEY_EQUALS = "=";
	public static final String KEY_ADD_VARIABLE = "+VAR";
	public static final String KEY_ADD_EQUATION = "+F(x)";

	public static final String KEY_CLEAR = "CLEAR";
	public static final String KEY_ABSOLUTE_VALUE = "abs";
	public static final String KEY_SQUARE_ROOT = "sqrt";
	public static final String KEY_EXCLAMATION = "!";
	public static final String KEY_SIN = "sin";
	public static final String KEY_COS = "cos";
	public static final String KEY_TAN = "tan";
	public static final String KEY_LN = "ln";
	public static final String KEY_LOG = "log";
	public static final String KEY_E = "e";
	public static final String KEY_PIE = "π";


	private TextView equationTextView;
	private TextView solutionTextView;

	private Button backButton;

	private String solution;

	private Stack<String> stack;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calc_layout);

		equationTextView = (TextView)findViewById(R.id.equation_textview);
		solutionTextView =(TextView)findViewById(R.id.solution_textview);

		backButton = (Button)findViewById(R.id.back_button);

		stack = new Stack<String>();

		solution = "";

		backButton.setOnLongClickListener( new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) 
			{
				stack.clear();
				solution = "";
				update();
				return true;
			}
		});

		/*		equationTextView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Layout layout = ((TextView) v).getLayout();
				int x = (int)event.getX();
				int y = (int)event.getY();
				if ( layout != null )
				{
					int line = layout.getLineForVertical(y);
					int offset = layout.getOffsetForHorizontal(line, x);
					Toast.makeText(EquationCalculator.this, offset+"", Toast.LENGTH_SHORT).show();
				}
				return false;
			}
		});*/

		update();

	}

	public void btnpress( View v )
	{

		Button buttonPressed = (Button) v;
		String text = buttonPressed.getText().toString();

		if( v.equals( backButton ) )
		{
			if( !stack.isEmpty() )
			{
				stack.pop();
			}

			solution = "";
		}
		else if( text.equals( KEY_CLEAR ) )
		{
			stack.clear();
			solution = "";
		}
		else if( text.equals( KEY_EQUALS ) )
		{
			String equation = getEquationString();

			if( equation.length() > 0 )
			{
				solution = solve( equation );
			}

		}
		else if( text.equals( KEY_ADD_VARIABLE ) )
		{

		}
		else if( text.equals( KEY_ADD_EQUATION ) )
		{

		}
		else if( isFunction( text ) )
		{
			if( !stack.isEmpty() && ( isNumber( stack.peek() ) || isVariable( stack.peek() ) ) )
			{
				stack.push( "*" );
			}
			stack.push( text + "(" );
		}
		else
		{
			if( isNumber(text) )
			{
				if( !stack.isEmpty() && ( stack.peek().equals(")") || isVariable(stack.peek()) ))
				{
					stack.push( "*" );
				}
			}
			else if( isVariable(text) )
			{
				if( !stack.isEmpty() && ( stack.peek().equals(")") || isVariable(stack.peek()) || isNumber(stack.peek()) ))
				{
					stack.push( "*" );
				}
			}

			stack.push( text );
		}
		update();
	}

	public String solve( String expression )
	{
		String solution = "";
		try 
		{
			Calculable calc = new ExpressionBuilder( expression )
			.withVariable("π", PI_VALUE)
			.withVariable("e", E_VALUE).build();

			solution = "= " + calc.calculate();

		} 
		catch (Exception e) 
		{
			solution = "ERROR";
			e.printStackTrace();
		} 

		return solution;
	}

	public String getEquationString()
	{
		String equation = "";

		for( int i=0; i < stack.size(); i++ )
		{
			equation = equation + stack.elementAt(i);
		}

		return equation;
	}

	public void update()
	{
		equationTextView.setText( getEquationString() );
		solutionTextView.setText( solution ); 
	}

	public boolean isNumber( String text )
	{
		if( text.equals("0") 
				|| text.equals("1") 
				|| text.equals("2") 
				|| text.equals("3") 
				|| text.equals("4") 
				|| text.equals("5") 
				|| text.equals("6") 
				|| text.equals("7") 
				|| text.equals("8") 
				|| text.equals("9") )
		{
			return true;
		}
		return false;
	}

	public boolean isVariable( String text )
	{
		if( text.equals("π") 
				|| text.equals("e") )
		{
			return true;
		}
		return false;
	}

	public boolean isFunction( String text )
	{
		if( text.equals(KEY_ABSOLUTE_VALUE) 
				|| text.equals(KEY_SQUARE_ROOT) 
				|| text.equals(KEY_SIN) 
				|| text.equals(KEY_COS) 
				|| text.equals(KEY_TAN) 
				|| text.equals(KEY_LOG) 
				|| text.equals(KEY_LN) )
		{
			return true;
		}
		return false;
	}
}